<?php
namespace App\Models\V2;
use DB;

class CommentModel{

    /**
     * 添加评论信息
     * @param integer $id 被评论的对象ID,有可能是文章，视频，音频，根据type而定
     * @param string $type 对象类型 HOT:热么书籍 STUDY:首页心灵文章 READ:未知 DAY_AUDIO:每日音频
     * @param string $content 评论内容
     * @param string $userName 评论者名称
     * @param integer $userId 评论者用户ID
     * */
    public function add_comment($id,$type,$content,$userName='',$userId=0){
        $data = [
            'pid'   => $id,
            'type'  => $type,
            'uid'   => $userId,
            'fid'   => '',
            'description'   => $content,
            'ip'            => get_real_ip(),
            'author'        => $userName,
            'created_at'    => date_time(),
            'updated_at'    => date_time()
        ];

        $result = DB::table('def_comment')->insert($data);
        return $result;
    }


    /**
     * 获取评论信息
     * @param integer $pid 对象ID
     * @param string $type 类型ID
     *
     * */
    public function get_comments($pid,$type){
        $colum  = [
            'a.pid',
            'a.uid',
            'a.description',
            'a.ip',
            'a.author',
            'a.created_at',
            'b.YourName',
            'b.head_img'
        ];

        $result = DB::table('def_comment as a')
            ->leftJoin('memberinfo as b','b.UserID','=','a.uid')
            ->where('a.pid',$pid)
            ->where('a.type',$type)
            ->select($colum)
            ->get();

        $today = "/".date('Y-m-d')."/";
        $yesDay= "/".date('Y-m-d',time()-24*60*60)."/";
        foreach($result as $com){
            $com->YourName  = $com->YourName ? $com->YourName : $com->author;
            $com->head_img  = change_member_head_img($com->head_img);
            if(preg_match($today,$com->created_at)){
                $com->dateName = "今天";
            }elseif(preg_match($yesDay,$com->created_at)){
                $com->dateName = "昨天";
            }else{
                $com->dataName = $com->created_at;
            }
        }
        return $result;
    }


    /**
     * 统计点赞数量
     * @param integer $pid 对象ID
     * @param string $type 类型ID
     * */
    public function count_good($pid,$type){
        $count = DB::table('def_favorite')->where('pid',$pid)->where('type',$type)->count();
        return $count;
    }



    /**
     * 点赞
     * */
    public function set_good($pid,$type,$userId = 0){
        $data = [
            'pid'   => $pid,
            'type'  => $type,
            'uid'   => $userId,
            'ip'    => get_real_ip(),
            'created_at'=>date_time(),
            'updated_at'=>date_time()
        ];
        $result  = DB::table('def_favorite')->insert($data);
        return $result;
    }

    /**
     * 检查是否进行过点赞
     * @param integer $pid 对象ID
     * @param string $type 类型
     * @param integer $userId 用户ID
     *
     * */
    public function check_is_set_good($pid,$type,$userId=0){
        $db = DB::table('def_favorite')->where('pid',$pid)->where('type',$type);
        if($userId > 0){
            $db->where('uid',$userId);
        }else{
            $ip = get_real_ip();
            $db->where('ip',$ip);
        }
        $result = $db->count();
        if($result > 0){
            return true;
        }else{
            return false;
        }
    }

}